package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.event.TimeOutEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wwq on 2017/4/7.
 */

public class BannerContentActivity extends BaseActivity {
    @BindView(R.id.leftButton)
    Button leftButton;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rightButton)
    Button rightButton;
    @BindView(R.id.titlebar)
    FrameLayout titlebar;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private LinearLayout ll_content;

    private String urlStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_banner_content);
        ButterKnife.bind(this);

        rightButton.setVisibility(View.GONE);

        // 取出数据
        urlStr = getIntent().getStringExtra("bannerUrl").toString();

        title.setText("详情");

        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        initUrl(urlStr);
    }

    @OnClick(R.id.leftButton)
    public void onClick() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initUrl(String url) {
        if (url.contains("<table")) {
            String[] urls = url.split("<table");
            for (int i = 0; i < urls.length; i++) {
                setUrlView(urls[i]);
            }
        } else {
            TextView headerText = new TextView(mActivity);
            headerText.setText(Html.fromHtml(url));
            ll_content.addView(headerText);
        }
    }

    private void setUrlView(String url) {
        if (url.contains("</table>")) {
            String[] arr2 = url.split("</table>");
            String str2 = "<table" + arr2[0] + "</table>";

            WebView webView = new WebView(mActivity);
            webView.loadDataWithBaseURL(null, str2, "text/html", "utf-8", null);
            ll_content.addView(webView);
            if (arr2.length > 1) {
                TextView headerText = new TextView(mActivity);
                headerText.setText(Html.fromHtml(arr2[1]));
                ll_content.addView(headerText);
            }
        } else {
            TextView headerText = new TextView(mActivity);
            headerText.setText(Html.fromHtml(url));
            ll_content.addView(headerText);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
